﻿using FluentValidation;
using System.Text.RegularExpressions;

namespace WebApi.Models.Views.Account
{
    /// <summary>
    /// 重置密码请求参数
    /// </summary>
    public class ResetPasswordRequest
    {
        /// <summary>
        /// 旧密码
        /// </summary>
        public string OldPassword { get; set; } = string.Empty;

        /// <summary>
        /// 新密码
        /// </summary>
        public string NewPassword { get; set; } = string.Empty;
    }

    /// <summary>
    /// 重置密码参数验证
    /// </summary>
    public class ResetPasswordValidator : AbstractValidator<ResetPasswordRequest>
    {
        /// <summary>
        /// 参数验证
        /// </summary>
        public ResetPasswordValidator()
        {
            RuleFor(p => p.OldPassword).NotNull().NotEmpty().WithMessage("旧密码必填");
            RuleFor(p => p.NewPassword).NotNull().NotEmpty().WithMessage("新密码必填");
            RuleFor(p => p.NewPassword).Must(p => CheckPassword(p)).WithMessage("新密码必须是英文大写、英文小写、数字和特殊字符组成的8-20位密码");
        }

        /// <summary>
        /// 密码检测
        /// </summary>
        /// <param name="pwd"></param>
        public bool CheckPassword(string pwd)
        {
            return Regex.IsMatch(pwd, @"^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*([\x21-\x2f]|[\x3a-\x40]|[\x5b-\x60])).{8,20}$");
        }
    }
}
